<html>
<head>
    <#assign basePath=request.contextPath>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>后台管理系统</title>
    <#include "/WEB-INF/views/template/bootMainCss.ftl" parse=true encoding="utf-8">
    <link href="${basePath}/bootstrap/css/form.lzy.css" rel="stylesheet">
    <style>
        .name {
            float: right;
        }

        .row {
            margin: 0;
        }

        .shop-img {
            text-align: left;
            padding-left: 30px;
        }

        .shop-img img {
            margin-right: 10px;
        }

        .panel-body {
            padding-bottom: 15px;
        }

        #tbodyId {
            font-size: 13px;
        }

        #myTable thead {
            font-size: 14px;
        }

        .list-img {
            text-align: left;
        }

        .list-img .s-img {
            float: left;
            width: 106px;
            height: 60px;
            margin: 0 10px 0 20px;
            _display: inline;
            overflow: hidden;
        }

        .list-img .s-img img {
            display: block;
            margin: 0 auto;
        }

        /*.list-img span{ line-height: 60px;}*/
    </style>
    <@shiro.hasPermission name="sys:agent:product:add">
        <input id="p_add" value="1" type="hidden"/>
    </@shiro.hasPermission>
    <@shiro.hasPermission name="sys:agent:product:update">
        <input id="p_update" value="1" type="hidden"/>
    </@shiro.hasPermission>
    <@shiro.hasPermission name="sys:agent:product:delete">
        <input id="p_delete" value="1" type="hidden"/>
    </@shiro.hasPermission>
</head>
<body>

<div class="panel panel-lzy">
    <div class="panel-body">
        <h5 class="tips-lzy">操作提示</h5>
        <ul class="tips-list-lzy">
            <li>◆&nbsp;<span>该页面显示了所有产品业务，可新增产品，供消费者购买。</span></li>
            <li>◆&nbsp;<span>可以对相关产品进行查看和编辑。</span></li>
        </ul>
    </div>
</div>

<div class="panel panel-f5">
    <div class="panel-body row name">
        <form id="myForm" class="form-inline form-margin-right">
            <div class="form-group">
                <span>产品名称：</span>
                <input type="text" class="form-control" id="name" size="32" placeholder="请输入产品名称关键字/产品创建人">
            </div>
            <div class="form-group">
                <button type="button" class="btn btn-primary btn-sm" onclick="search(1)">查询</button>
            </div>
        </form>
    </div>
<div class="panel-heading row">
    <@shiro.hasPermission name="sys:agent:product:add">
        <button type="button" class="btn btn-primary" onclick="addMeal();"><span class="fa fa-plus"></span> 新增产品
        </button>
    </@shiro.hasPermission>
    <@shiro.hasPermission name="sys:agent:product:delete">
        <button type="button" class="btn btn-primary" onclick="batchDown();">批量删除</button>
    </@shiro.hasPermission>
</div>
    <!-- 分页查询的地址-->
    <div class="panel-body panel-white">
        <div class="table-responsive">
            <table id="myTable"
                   class="table table-hover"
                   data-url="${basePath}/admin/agent/product/queryAgentProductList?type=1"
                   data-pageSize="10,50,100" data-method="GET">
                <thead>
                <!-- 需要循环的字段  对应mapper文件-->
                <tr>
                    <th data-type="checkbox" data-field="id"></th>
                    <th data-field="id">ID</th>
                    <th width="300s" data-field="name" data-call="true">产品信息</th>
                    <th data-field="createUserName" width="15%">创建人</th>
                    <th data-field="createTime" data-call="true" width="25%">创建时间</th>
                    <th data-field="price">产品售价</th>
                    <th data-field="button" data-call="true">操作</th>
                </tr>
                </thead>
                <tbody id="tbodyId">

                </tbody>
            </table>
        </div>
        <!-- 分页条-->
        <div class="row">
            <div align="left" class="col-xs-6">
                <div class="input-group">
                    <span class="input-group-addon" id="pageCount"></span> <select
                        id="pageSize" onchange="search(1)" class="form-control"
                        style="width: 100px"></select>
                </div>
            </div>
            <div align="right" class="col-xs-6">
                <ul class="pagination" id="pagination"></ul>
            </div>
        </div>
    </div>
</div>
<#include "/WEB-INF/views/template/bootMainJs.ftl" parse=true encoding="utf-8">
<script type="text/javascript" src="${basePath}/bootstrap/js/utils.js" type="text/javascript"></script>
<script type="text/javascript" src="${basePath}/bootstrap/plugin/jqPaginator/jqPaginator.js"></script>
<script type="text/javascript" src="${basePath}/bootstrap/plugin/jqPaginator/myPage.js"></script>
<script type="text/javascript">
    //循环列表数据以及操作按钮
    function tableCallBack(data, id) {
        if (id == 'button') {//增加操作按钮
            var count = 0;
            var a = $('#p_update').val();
            var _button = '<button type="button" class="btn btn-white btn-xs" onclick="viewMeal(' + data.id + ');">查看 </button> ';
            if ($('#p_update').val() == 1) {
                _button += '<button type="button" class="btn btn-white btn-xs" onclick="editMeal(' + data.id + ');">编辑</button> ';
            }
            if ($('#p_delete').val() == 1) {
                _button += '<button type="button" class="btn btn-white btn-xs" onclick="deleteCha(' + data.id + ');">删除</button> ';
                count++
            }
            return _button;
        }
        else if (id == "name") {
            if (data.cover) {
                return "<div class='list-img'><span class='s-img'><img src=" + data.cover + " height='60' width='80'></span><span> " + data.name + "</span></div>"
            }
            else {
                return data.name
            }
        }
        else {
            if (data[id] == null || data[id] == '') {
                return '';
            } else {
                return data[id];
            }
        }
    }

    //详情
    function viewMeal(id) {
        window.location.href = '${basePath}/admin/agent/product/detailView/' + id;
    }

    //新增
    function addMeal() {
        window.location.href = '${basePath}/admin/agent/product/addView';
    }

    //编辑
    function editMeal(id) {
        window.location.href = '${basePath}/admin/agent/product/editView/' + id;
    }

    //删除
    function deleteCha(id) {
        layer.confirm('是否确定删除该产品？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: 'GET',
                url: '${basePath}/admin/agent/product/deleteAagntProduct?id=' + id,
                success: function (data) {

                    if(typeof data !== 'object'){
                        data= JSON.parse(data);
                    }

                    if (data.code == '0') {
                        layer.alert('删除成功!', {
                            icon: 6
                        }, function (index) {
                            layer.closeAll('dialog');
                            search(parseInt($('.active').attr('jp-data')));
                        });
                    }else {
                        layer.alert(data.desc, {
                            icon: 5
                        });
                    }
                }
            });
        });
    }


    //批量删除
    function batchDown() {
        var chk_value = [];
        $("#tbodyId").find("input").each(function () {
            if ($(this).attr('type') == 'checkbox' && $(this).is(':checked')) {
                chk_value.push($(this).val());
            }
        });
        if (chk_value.join(",") == null || chk_value.join(",") == '') {
            layer.alert('请选择需要删除的产品', {icon: 5});
            return;
        }
        layer.confirm('是否确定删除所选产品？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: 'POST',
                url: '${basePath}/admin/agent/product/deleteAagntProductList?ids=' + chk_value,
//                data:{ids :chk_value.join(","),status:0},
//                dataType: 'json',
                success: function (data) {
                    var data = JSON.parse(data);
                    if (data.code == '0') {
                        layer.alert('批量删除成功!', {
                            icon: 6
                        }, function (index) {
                            layer.closeAll('dialog');
                            search(parseInt($('.active').attr('jp-data')));
                        });
                    } else {
                        layer.alert(data.desc, {
                            icon: 56
                        });
                    }
                }
            });
        });
    }
</script>
</body>
</html>